package com.daocao.ds.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.adapter.LoanerInfoAdapter;
import com.daocao.ds.adapter.LoanerInfoTotalAdapter;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.IdBean;
import com.daocao.ds.api.model.DealDetailModel;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.util.DateTime;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.Spans;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.view.StarNumberView;
import com.daocao.ds.view.V19FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyOrderDetailActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.txt_realname)
    TextView txtRealname;
    @BindView(R.id.img_star)
    StarNumberView imgStar;
    @BindView(R.id.txt_number)
    TextView txtNumber;
    @BindView(R.id.txt_time)
    TextView txtTime;
    @BindView(R.id.txt_borrow_money)
    TextView txtBorrowMoney;
    @BindView(R.id.txt_borrow_date)
    TextView txtBorrowDate;
    @BindView(R.id.txt_borrow_use)
    TextView txtBorrowUse;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.txt_info_status)
    TextView txtInfoStatus;
    @BindView(R.id.txt_info_rest_date)
    TextView txtInfoRestDate;
    @BindView(R.id.txt_info_borrow_date)
    TextView txtInfoBorrowDate;
    @BindView(R.id.txt_info_return_date)
    TextView txtInfoReturnDate;
    @BindView(R.id.layout_info)
    LinearLayout layoutInfo;
    @BindView(R.id.txt_age)
    TextView txtAge;
    @BindView(R.id.txt_location)
    TextView txtLocation;
    @BindView(R.id.txt_education)
    TextView txtEducation;
    @BindView(R.id.txt_name)
    SuperButton txtName;
    @BindView(R.id.list_one)
    RecyclerView listOne;

    private Unbinder unbiner;
    private LoanerInfoTotalAdapter adapter;
    private LoanerInfoAdapter head_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_detail);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("订单详情");
        String id = getIntent().getStringExtra("id");

        head_adapter = new LoanerInfoAdapter(this);
        list.setAdapter(head_adapter);

        adapter = new LoanerInfoTotalAdapter(this);
        listOne.setAdapter(adapter);

        list.setNestedScrollingEnabled(false);
        listOne.setNestedScrollingEnabled(false);
        getOrderDetail(id);
    }

    public void getOrderDetail(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        IdBean bean = new IdBean();
        bean.id = id;
        Api.api_service.getDealDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });
    }

    private void setData(DealDetailModel.DatasBean item) {
        //借款状态： 1 已回款、2 使用中、3 回款期、4 已逾期
        switch (item.deal.deal_status) {
            case "1":
                txtInfoStatus.setText("已回款");
                layoutInfo.setBackgroundResource(R.drawable.btn_yihuikuan);
                txtInfoRestDate.setText("");
                break;
            case "2":
                txtInfoStatus.setText("正在使用中");
                layoutInfo.setBackgroundResource(R.drawable.btn_main_color_10dp);
                break;
            case "3":
                txtInfoStatus.setText("回款期");
                layoutInfo.setBackgroundResource(R.drawable.btn_huikuanqi);
                txtInfoRestDate.setText("逾期天数：" + item.deal.overdue_days + "天");
                break;
            case "4":
                txtInfoStatus.setText("已逾期");
                layoutInfo.setBackgroundResource(R.drawable.btn_yuqi);
                txtInfoRestDate.setText("逾期天数：" + item.deal.overdue_days + "天");
                break;
        }
        txtInfoReturnDate.setText("回款日期:" + item.deal.repay_date);
        txtInfoBorrowDate.setText("出借日期:" + item.deal.start_date);




        txtRealname.setText(item.user.real_name);
        if (item.deal.number.equals("0")) {
            txtNumber.setText("首次");
            txtNumber.setBackgroundResource(R.mipmap.img_home_number_select);
        } else {
            txtNumber.setText(item.deal.number + "次");
            txtNumber.setBackgroundResource(R.mipmap.img_home_number_unselect);
        }
        if (TextUtils.isEmpty(item.deal.level_id)) {
            item.deal.level_id = "0";
        }
        imgStar.setStar(Integer.valueOf(item.deal.level_id));

        if (TextUtils.isEmpty(item.deal.created_at)) {
            item.deal.created_at = "0";
        }
        txtTime.setText("申请时间：" + DateTime.toDate(Long.valueOf(item.deal.created_at) * 1000));

        if (TextUtils.isEmpty(item.deal.borrow_amount)) {
            item.deal.borrow_amount = "0";
        }
        Spanned txt_amount = Spans.with(this).font(item.deal.borrow_amount, getResources().getDimensionPixelSize(R.dimen.x32), 0xff0072FF).style(Typeface.BOLD).
                font("\n借款金额", getResources().getDimensionPixelSize(R.dimen.x24), 0xff999999).
                build();
        txtBorrowMoney.setText(txt_amount);

        if (TextUtils.isEmpty(item.deal.limit_days)) {
            item.deal.limit_days = "0";
        }
        Spanned txt_limit_day = Spans.with(this).font(item.deal.limit_days + "天", getResources().getDimensionPixelSize(R.dimen.x32), 0xff333333).style(Typeface.BOLD).
                font("\n借款期限", getResources().getDimensionPixelSize(R.dimen.x24), 0xff999999).
                build();
        txtBorrowDate.setText(txt_limit_day);

        if (TextUtils.isEmpty(item.deal.use_type)) {
            item.deal.use_type = "无";
        }
        Spanned txt_use = Spans.with(this).font(item.deal.use_type, getResources().getDimensionPixelSize(R.dimen.x32), 0xff333333).style(Typeface.BOLD).
                font("\n借款用途", getResources().getDimensionPixelSize(R.dimen.x24), 0xff999999).
                build();
        txtBorrowUse.setText(txt_use);


        txtAge.setText(item.user.age);
        txtLocation.setText(item.user.address);
        txtEducation.setText(item.user.education);


        head_adapter.update(item.base_info.lists);

        List<DealDetailModel.DatasBean.BaseInfoBean> data = new ArrayList<>();
        data.add(item.expense);
        data.add(item.assets);
        data.add(item.socialsecurity);
        data.add(item.risk);
        data.add(item.credit);
        adapter.update(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;

        }
    }
}
